package demo.jdbc;

import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author : GuoFei
 * @date : 2019/5/30 16:10
 */
@Slf4j
@Component
public class TestJDBCTemplate {

//    @Autowired
//    private DataSource dataSource ;

    public static MyDao.SqlServerConnInfo info() throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream(ResourceUtils.getFile("classpath:properties/local/jdbc.properties")));
        return new MyDao.SqlServerConnInfo(properties.getProperty("jdbc.driver"),properties.getProperty("jdbc.url"),properties.getProperty("jdbc.username"),properties.getProperty("jdbc.password"));
    }

    // 获取连接
    public Connection getConn() throws SQLException, IOException, ClassNotFoundException {
        MyDao.SqlServerConnInfo connInfo = info();
        Class.forName(connInfo.getDriver());
        return DriverManager.getConnection(connInfo.getUrl(),connInfo.getUsername(),connInfo.getPassword());
    }

    public JSONArray list()  {

//        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//        jdbcTemplate.setDataSource(dataSource);
//        System.out.println("jdbc:" + jdbcTemplate);
        String sql = "select top 10 iD,New_name,create_Time from userTest where id > ? order by id desc ; ";

//        List<Object> data = jdbcTemplate.query(sql);




        return new JSONArray();
//        Connection connection = getConn();
//        String sql = "select top 10 iD,New_name,create_Time from userTest where id > ? order by id desc ; ";
//        PreparedStatement preparedStatement = connection.prepareStatement(sql);
//        preparedStatement.setObject(1, 2000);
//        ResultSet resultSet = preparedStatement.executeQuery();
//        JSONArray ja = new JSONArray();
//        JSONObject jo;
//        ResultSetMetaData data = resultSet.getMetaData();
//        log.info(JSON.toJSONString(data));
//        for (int i = 0; i < data.getColumnCount(); i++) {
//            System.out.println("1_"+data.getColumnLabel(i+1));
//            System.out.println("2_"+data.getColumnName(i+1));
//            System.out.println("3_"+data.getColumnType(i+1));
//            System.out.println("4_"+data.getColumnTypeName(i+1));
//            System.out.println("7_"+data.getColumnClassName(i+1));
//            System.out.println("8_"+data.getColumnDisplaySize(i+1));
//            System.out.println("9_"+data.getScale(i+1));
//            System.out.println("_____------------");
//        }
//        while (resultSet.next()) {
//            jo = new JSONObject();
//            jo.put("id", resultSet.getInt("id"));
//            jo.put("name", resultSet.getString("new_name"));
//            jo.put("create_time1", resultSet.getString("create_time"));
//            jo.put("create_time2", resultSet.getDate("create_time"));
//            jo.put("create_time3", resultSet.getTime("create_time"));
//            jo.put("create_time4", resultSet.getTimestamp("create_time"));
//            ja.add(jo);
//        }
//        connection.close();
//        resultSet.close();
//        return ja;
    }
}
